package com.mccken.demo.collect.collect_01;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;

import lombok.SneakyThrows;

/**
 * @program: mccken-collect
 * @description:
 * @author: mccken
 * @create: 2024-01-08 11:20
 **/
public class MyCompletionServiceDemo {

	public static void main1(String[] args) {
		// 快速创建线程池
		ExecutorService executor = Executors.newFixedThreadPool(3);

		CompletableFuture<Void> voidCompletableFuture = CompletableFuture.runAsync(() -> {
			System.out.println(100);
		}, executor);
		
		
		// 创建CompletionService
		CompletionService<Integer> cs = new ExecutorCompletionService<>(executor);
		Integer s1 = 0;
		cs.submit(() -> getPriceByS1(), s1);
		cs.submit(() -> getPriceByS2());
		cs.submit(() -> getPriceByS3());
		for (int i = 0; i < 3; i++) {
			try {
				Integer i1 = cs.take().get();
				System.out.println(i1);
				System.out.println(s1);
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			} catch (ExecutionException e) {
				System.out.println(e);
			}
		}

		executor.shutdown();
		System.out.println(s1);
		System.out.println("down");
	}

	private static Integer getPriceByS1() {
		return 1;
	}
	@SneakyThrows
	private static Integer getPriceByS2() {
		Thread.sleep(1000 * 3);
		return 2;
	}
	private static Integer getPriceByS3() {
		throw new RuntimeException();
	}

	public static void main(String[] args) {
		doGet();
	}

	public static String doGet() {
		ExecutorService threadPool1 = new ThreadPoolExecutor(3, 5, 0L, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<>(100));

		AtomicInteger i1 = new AtomicInteger(0);
		
		Supplier<String> child1 = () -> {
			int i = i1.incrementAndGet();
			if (i >4) {
				System.out.println();	
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				throw new RuntimeException(e);
			}
			Supplier<String> child = () -> {
				if (i >4) {
					System.out.println();
				}
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					throw new RuntimeException(e);
				}
				System.out.println("child");
				return "child";
			};
			return CompletableFuture.supplyAsync(child, threadPool1).join();//子任务
		};
		
		new Thread() {
			@Override
			public void run() {
				while (true) {

					try {
						Thread.sleep(1000);
					} catch (InterruptedException e) {
						throw new RuntimeException(e);
					}


					System.out.println(threadPool1);
				}
			}
		}.start();
		
		List<CompletableFuture> list = new ArrayList<>();
		for (int i = 0; i < 10; i++) {
			CompletableFuture cf1 = CompletableFuture.supplyAsync(child1, threadPool1);
			list.add(cf1);
		}

		for (CompletableFuture completableFuture : list) {
			Object join = completableFuture.join();
			System.out.println(join);
		}
		
		return null;
	}

}
